import java.io.*;
import java.util.Iterator;
import java.util.List;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.util.ArrayList;

public class EjemploXML {
	public static void main(String[] args) {
		try {
			SAXBuilder builder=new SAXBuilder(false);
			Document request = builder.build("res/requestExample.xml");
			Document structure = builder.build("res/structure.xml");
			
			Element requestRoot = request.getRootElement();
			Element structureRoot = structure.getRootElement();
			
			ArrayList<String> fieldList = new ArrayList<String>();
			Iterator temp = structureRoot.getChildren("Field").iterator();
			while(temp.hasNext())
			{
				fieldList.add(((Element)temp.next()).getValue());
			}
			
			System.out.println("El request es de tipo: "+requestRoot.getAttributeValue("tipo"));
						
			List alumnos = requestRoot.getChildren("Alumno");
			System.out.println("Numero de elementos a procesar: "+alumnos.size());
			Iterator alumnosI = alumnos.iterator();
			
			
			
			while(alumnosI.hasNext())
			{
				System.out.println(structureRoot.getAttributeValue("structureName"));
				Element alumno = (Element)alumnosI.next();
				
				List datos = alumno.getChildren();
				Iterator datosI = datos.iterator();
				
				System.out.println("Datos asociados: ");
				while(datosI.hasNext())
				{
					Element dato = (Element)datosI.next();
					if(fieldList.contains(dato.getName()))
						System.out.println("\t"+dato.getName()+": "+dato.getValue());
					else
						System.out.println("\t"+dato.getName()+" no es un campo valido");
				}
			}
			
		}catch (Exception e){
			e.printStackTrace();
		}
	}
}
